package com.situ.stmall.manager.mapper;

import com.situ.stmall.manager.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 不用xml 开发，
 * 使用基于注解开发
 */
@Mapper   //MyBatis 的注解,相当于Application添加MapperScan注解
public interface CategoryMapper {
    /**
     * 插入；返回值类型 int
     * 方法名：insert
     * 参数：Category
     * 配置：使用@Insert配置sql语句
     */
    @Insert({"insert into category",
            "(name,dscp,pic,parent_id,recom)",
            "value(#{name},#{dscp},#{pic},#{parentId},#{recom})"})
    /**
     *     获取自增主键
     */
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insert(Category category);

    /**
     * 根据id删除
     */
    @Delete("delete from category where id = #{id}")
    int delete(Integer id);

    /**
     * 根据id更新,使用动态SQL，需要为SQL语句添加<script></script>
     */
    @Update({"<script>",
            "update category",
            "<set>",
            "<if test='name !=null and name.length>0' > name=#{name}, </if>",
            "<if test='dscp !=null and dscp.length>0' > dscp=#{dscp}, </if>",
            "<if test='pic !=null and pic.length>0' > pic=#{pic}, </if>",
            "<if test='parentId !=null' > parent_id=#{parentId}, </if>",
            "<if test='recom !=null' > recom=#{recom} </if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    int update(Category category);

    /**
     * 根据id查询
     */
    @Select("select * from category where id = #{id}")
    Category selectById(Integer id);

    /**
     * 通过多条件组合查询
     */
    @Select({"<script>",
            "select * from category",
            "<where>",
            "<if test='name !=null and name.length>0' > name like '%${name}%' </if>",
            "<if test='parentId !=null and parentId != 0' > and parent_id=#{parentId} </if>",
            "<if test='parentId !=null and parentId == 0' > and parent_id is null </if>",
            "<if test='recom !=null' > and recom=#{recom} </if>",
            "<if test='status !=null' > and status=#{status} </if>",
            "</where>",
            "</script>"})
    @Results({ //相当于Resultmap的标签
            @Result(column = "parent_id",property = "parentId"),
            @Result(column = "parent_id",property = "parent",
            one = @One(select = "selectById"))  //相当于<id> <result>
    })
    List<Category> select(Category category);

}
